Difference between <context:annotation-config> and <context:component-scan>

<context:annotation-config>

<context:component-scan>

<context:annotation-config> can resolve are @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource

<context:component-scan> may identify a number of them, including @Component, @Repository, @Service, @Controller, @RestController, and @Configuration.

In context, the beans are notregistered by the component-config.

In context, the beans are registered by the component-scan.

Determining beans and their dependencies is done using context:annotation-config.

Context:component-scan is used for general-purpose component auto-detection.

Annotations in beans that are already registered in the application context are activated using <context:annotation-config>.

Similar to <context:annotation-config>, <context:component-scan> may likewise search packages for beans inside the application context and register them.

Spring MVC context:annotation-config VS context:component-scan

In this article, we will learn about Spring MVC <context:annotation-config> vs <context:component-scan>. Activating all annotations existing in Java beans that have already been registered either by definition in an application context file or by component scanning is the primary function of annotation-config. The requirement for registration is crucial. The component scan assigns the Spring Bean registration to Java classes annotated with @component, @service, @repository, and other attributes. It can do all the functions that annotation config can.

Similar Reads

Annotations in beans previously registered in the application context can be activated using context:annotation-config, regardless of whether they were specified using XML or via package scanning. This implies that for beans that have already been produced and saved in the spring container, it will resolve the @Autowired and @Qualifier annotations....

...

Difference between and

The context:component-scan tag does the same functions as context:annotation-config, plus it scans the packages and marks the stereotype-annotated classes as spring beans. Declaring each bean in the XML files would be unnecessary if the spring configuration file had the context:component-scan element....

Conclusion

...